/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.preference;

import java.util.ArrayList;
import java.util.List;

import com.wirelesstransfer.R;
import com.wirelesstransfer.activity.dialogs.Question;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;

public class Preference extends Activity implements OnClickListener{

	public static final int DIALOG_NON_INTEGER = 0;
	
	private EditText uploadEditText;
	private EditText downloadEditText;
	private EditText maxSizeEditText;
	private EditText nrConnectionsEditText;

	private CheckBox uploadCheckBox;
	private CheckBox downloadCheckBox;
	private CheckBox maxSizeCheckBox;
	private CheckBox nrConnectionsCheckBox;
	
	private Resources res;
	
	// Alpha values for the EditText fields
	public static final int ALPHAFOCUS = 255;
	public static final int ALPHANOTFOCUS = 150;
	
	private List<EditText> preferenceFields;		// List of all the preference values
	private PreferenceValues preferenceValues;		// Class which handles all preference values


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preference);
		
        res = getResources();

		// Click listeners for buttons
		View setDefaultButton = findViewById(R.id.btn_setdefault);
		setDefaultButton.setOnClickListener(this);
		View applyButton = findViewById(R.id.btn_apply);
		applyButton.setOnClickListener(this);

		// Create instance for EditText fields
		uploadEditText = (EditText) findViewById(R.id.uploadspeed_edit);
		downloadEditText = (EditText) findViewById(R.id.downloadspeed_edit);
		maxSizeEditText = (EditText) findViewById(R.id.maxsize_edit);
		nrConnectionsEditText = (EditText) findViewById(R.id.numberconnections_edit);

		// Create instance for CheckBoxes
		uploadCheckBox = (CheckBox) findViewById(R.id.uploadspeed_cbx);
		uploadCheckBox.setOnClickListener(this);
		downloadCheckBox = (CheckBox) findViewById(R.id.downloadspeed_cbx);
		downloadCheckBox.setOnClickListener(this);
		maxSizeCheckBox = (CheckBox) findViewById(R.id.maxsize_cbx);
		maxSizeCheckBox.setOnClickListener(this);
		nrConnectionsCheckBox = (CheckBox) findViewById(R.id.numberconnections_cbx);
		nrConnectionsCheckBox.setOnClickListener(this);
	
		// Add all fields to the PreferenceFields class
		preferenceFields = new ArrayList<EditText>();
		preferenceFields.add(uploadEditText);
		preferenceFields.add(downloadEditText);
		preferenceFields.add(maxSizeEditText);
		preferenceFields.add(nrConnectionsEditText);
		
		preferenceValues = new PreferenceValues(preferenceFields);		

	}

	public void onClick(View v) {
		boolean focus = true;
		switch(v.getId()){
		case R.id.btn_setdefault:
			boolean getValues;
			getValues = preferenceValues.getValues();
			if (!getValues)
				showDialog(DIALOG_NON_INTEGER);
			break;
		case R.id.btn_apply:
			break;	
		}
		
		// Buggy
		if (uploadCheckBox.isChecked()) //&& ((CheckBox) v).isChecked())
			setWritablePreference(uploadEditText, focus);
		else if (downloadCheckBox.isChecked() )//&& ((CheckBox) v).isChecked())
			setWritablePreference(downloadEditText, focus);
		else if (maxSizeCheckBox.isChecked()) // && ((CheckBox) v).isChecked())
			setWritablePreference(maxSizeEditText, focus);
		else if (nrConnectionsCheckBox.isChecked()) // && ((CheckBox) v).isChecked())
			setWritablePreference(nrConnectionsEditText, focus);
		else{
			focus = false;
			setWritablePreference(downloadEditText, focus);
			setWritablePreference(uploadEditText, focus);
			setWritablePreference(maxSizeEditText, focus);
			setWritablePreference(nrConnectionsEditText, focus);
		}
	
	}
	
	/**
	 * Set focusable and alpha on EditText Fields
	 * @param textField 
	 * @param isWritable
	 */
	private void setWritablePreference(EditText textField, boolean isWritable){
		textField.setFocusable(isWritable);
		textField.setFocusableInTouchMode(isWritable);
		
		if(isWritable)
			textField.getBackground().setAlpha(ALPHAFOCUS);
		else
			textField.getBackground().setAlpha(ALPHANOTFOCUS);
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.questionmenu, menu);
        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.question_menu:
	    	Intent question = new Intent(this, Question.class);
	    	question.putExtra(res.getString(R.string.question_TITLE),res.getString(R.string.question_preference_title_txt));
	    	question.putExtra(res.getString(R.string.question_TEXT),res.getString(R.string.question_preference_txt));
	    	startActivity(question);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;
		
		switch (id) {
		case DIALOG_NON_INTEGER:
			dialog = buildOkDialog(res.getString(R.string.preference_NO_POSITIVE_INT));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;
		
	}
	
	/**
	 * Creates text in a dialog with Ok key
	 * @param action
	 * @return
	 */
	private AlertDialog buildOkDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		       .setCancelable(false)
		       
		       .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		return alert;
	}

}
